import { reqGoodsInfo, reqAddCart } from "@/api"
import { getUUID } from '@/utils'

const state = {
    goodsInfo: {},
    uuid_token:getUUID()
}
const mutations = {
    GETGOODSINFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    },
    GETADDCART(state, addCart) {
        state.addCart = addCart
    }
}
const actions = {
    // 获取产品信息的action
    async getGoodsInfo({ commit }, skuId) {
        let res = await reqGoodsInfo(skuId)
        if (res.code == 200) {
            commit('GETGOODSINFO', res.data)
        }
    },
    async getAddCart({ commit }, {skuId, skuNum}) {
        let res = await reqAddCart(skuId, skuNum)
        if (res.code !=200) return Promise.reject(new Error('faile'))
        return 'success'
    }
}
const getters = {
    categoryView(state) {
        return state.goodsInfo.categoryView || {}
    },
    skuInfo() {
        return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList() {
        return state.goodsInfo.spuSaleAttrList || []
    }
}

export default {
    state, mutations, actions, getters
}